
package com.dave.gd.common.util.xml.model;

import java.util.LinkedList;

import org.dom4j.Element;

/**
 * XmlBean的list容器
 * 
 * 
 * @param <T>
 */
abstract class XmlBeanList<T extends XmlBean> extends LinkedList<T> implements
		XmlCapacity {
	private static final long serialVersionUID = 8908864749922551591L;

	public XmlBeanList() {
		super();
	}

	/**
	 * 实例化List容器
	 * 
 
	 */
	public abstract XmlCapacity newInstance();

	/**
	 * 获取一个实例化元素
	 * 
	 * @return XmlBean对象
	 */
	public abstract XmlBean newEleInstance();

	/**
	 * (non-Javadoc)
	 * 
	 
	 */
	@Override
	public void serialize(Element parentEle, String localName) {
		for (XmlBean bean : this) {
			if (bean == null) {
				continue;
			}
			parentEle.add(bean.serialize(localName));
		}
	}

	/**
	 * (non-Javadoc)
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void unserialize(Element localEle) {
		if (localEle == null
				|| !(localEle.elementIterator().hasNext() || localEle
						.attributeIterator().hasNext())) {
			// 没子节点也没属性
			return;
		}

		XmlBean bean = newEleInstance();
		bean.unserialize(localEle);
		this.add((T) bean);
	}
}
